package cn.kgc.jdktest.demo11;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author:yaozhaobao
 * @Description:TODO
 * @version:1.0
 */
public class TestData {

    public static void main(String[] args) throws FileNotFoundException {
        // 准备数据
        List<UserExcel> userExcels = getUserExcelData();
        // 导出
        ExportParams params = new ExportParams();
        params.setTitle("远大公司用户信息列表");
        params.setSheetName("用户信息");
        params.setHeight((short) 15);

        int totalPage = (userExcels.size() / 10000) + 1;
        int pageSize = 10000;
        Workbook workbook = ExcelExportUtil.exportBigExcel(params,UserExcel.class, new IExcelExportServer() {
                    /**
                     * obj 就是下面的totalPage，限制条件
                     * page 是页数，他是在分页进行文件转换，page每次+1
                     */
                    @Override
                    public List<Object> selectListForExcelExport(Object obj, int page) {

                        //很重要！！这里面整个方法体，其实就是将所有的数据aList分批返回处理
                        //分批的方式很多，我直接用了subList。然后 每批不能太大。我试了30000一批，
                        //内存溢出，貌似 最大每批10000


                        if (page > totalPage) {
                            return null;
                        }

                        // fromIndex开始索引，toIndex结束索引
                        int fromIndex = (page - 1) * pageSize;
                        int toIndex = page != totalPage ? fromIndex + pageSize :userExcels.size();

                        List<Object> list = new ArrayList<>();
                        list.addAll(userExcels.subList(fromIndex, toIndex));

                        return list;
                    }
                }, totalPage);
        // 指定写出的文件
        FileOutputStream outputStream = new  FileOutputStream("D:\\easypoi_big.xlsx");
        try {
            workbook.write(outputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static List<UserExcel> getUserExcelData() {
        List<UserExcel> userExcels = new ArrayList<>();
        List<Order> orders = new ArrayList<>();
        for (int i = 1; i <=2 ; i++) {
            Order order = new Order("no1234" + i, "商品" + i);
            orders.add(order);
        }
        // 10万记录写入
        for (int i = 1; i <= 100000; i++) {
            UserExcel user = UserExcel.builder()
                    .id(i)
                    .name("admin" + i)
                    .age(i * 10)
                    .gender(i % 2 == 0 ? "男" : "女")
                    .birthday(new Date())
                    .hobbys(Arrays.asList("打篮球", "听英语", "看书"))
                    .card(new Card("43082119930505" + i, "广东深圳"))
                    .orders(orders)
                    .build();
            userExcels.add(user);
        }
        return userExcels;
    }

}
